var express = require('express');
var Users = require('../models/Users');
var router = express.Router();
var url=require("url");
var ucheck = require('../utils/ucheck');

var pwdHash = require("../utils/pwd");

/* GET users listing. */
router.get('/debug',function (req,res) {
    Users.createUnique({
        username : 'admin1',
        passwd : 'admin2017',
        nickname : '临时管理员',
        root : true
    });
});

// 登录界面
router.get('/login',function (req,res,next) {
    var redirect = req.query.redirect || '/';
    res.render('user/login',{title:'用户登录',redirect:redirect});
});

router.post('/login',function (req,res,next) {
    var username = req.body['username'];
    var password = req.body['password'];

    if (username.length === 0 || password.length === 0) {
        return res.json({
            message : '用户名、密码不能为空',
            code : -1,
            data : null
        });
    }
    Users.getLoginInfo(username,function (err,user) {

        if (err || !user) {
            return res.json({
                message : '用户名或密码不正确',
                code : -1,
                data : null
            });
            // return res.redirect('/user/login');
        }
        if (pwdHash(password) === user.passwd) {
            req.session.uid = user._id;
            req.flash('success', '登录成功');
            return res.json({
                message : '成功',
                code : 0,
                data : null
            });
        } else {
            // req.flash('error', '密码不正确');
            return res.json({
                message : '密码不正确',
                code : -1,
                data : null
            });
        }
    })
});

router.get('/logout',function (req,res,next) {
    req.session.uid = null;
    req.flash('success', '已退出登录');
    res.redirect('/user/login');
});

router.get('/apply',function (req,res) {
    if (req.session.uid) {
        return res.redirect('/');
    }
    res.render('user/apply',{'title':'申请账号'})
});

router.get('/center/:username',function (req,res,next) {
    var username = req.params.username;

    ucheck.isUser(username,res,function (isUser) {
        if (isUser) {
            res.render('user/center',{title:'个人中心'});
        } else  {
            req.flash('error', '对不起，您无权访问');
            res.redirect('/');
        }
    });
});

router.post('/update',function (req,res,next) {
    var uid = req.session.uid;
    if (!uid) {
        return res.json({
            message : '请登录后操作',
            code : -1,
            data : null
        });
    }

    var nickname = req.body['nickname'];
    if (nickname === null || nickname.length === 0) {
        return res.json({
            message : '昵称不能为空',
            code : -1,
            data : null
        });
    }

    var user = res.locals.user;
    if (!user) {
        return res.json({
            message : '请登录后操作',
            code : -1,
            data : null
        });
    }
    user.nickname = nickname;

    Users.edit(uid,{nickname:nickname},function (err,newUser) {
        if (err) {
            return res.json({
                message : err.message,
                code : -1,
                data : null
            });
        }
        return res.json({
            message : '成功',
            code : 0,
            data : null
        });
    })
});

module.exports = router;
